package com.adkfp.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.adkfp.bean.ADKUserPhotoBean;
import com.adkfp.service.IADKUserPhotoService;
import com.adkfp.utils.PropertyUtil;

/**
 * class name:FileAction <BR>
 * class description: 文件操作 <BR>
 * Remark: <BR>
 * 
 * @version 1.00 2018年10月23日
 * @author Jack)jackwei
 */
@Controller
@RequestMapping("file")
public class FileAction {
	@Resource(name="aDKUserPhotoService")
	private IADKUserPhotoService aDKUserPhotoService;
	/**
	 * Method name: uploadOneImageAjax <BR>
	 * Description: 上传一个图片文件 <BR>
	 * Remark: <BR>
	 * @param file
	 * @param request
	 * @return
	 * @throws IOException  String<BR>
	 */
	@RequestMapping("uploadOneImageAjax")
	@ResponseBody
	public String uploadOneImageAjax(@RequestParam("file") MultipartFile file,ADKUserPhotoBean bean,String userid) throws IOException {
		try {
			InputStream in = file.getInputStream();
			//获取服务器存放文件路径
			String path = PropertyUtil.getConfigureProperties("upload");
			System.out.println("aaaaaaa-------------"+path);
			//文件名
			String fileName = file.getOriginalFilename();
			/*if(userid!=null) {
				System.out.println("bbbbbbbbbbb-------------"+userid);
				bean.setPhoto(path+"/"+fileName);
				aDKUserPhotoService.update(bean);
			}else {*/
				System.out.println("ccccccccccccccc-------------"+userid);
				bean.setPhoto(path+"/"+fileName);
				bean.setUserid(userid);
				aDKUserPhotoService.insert(bean);
			/*}*/
			File newFilie = new File(path, fileName);
			FileOutputStream out = new FileOutputStream(newFilie);
			byte[] arrs = new byte[1024];
			int len = 0;
			while((len=in.read(arrs))!=-1) {
				out.write(arrs, 0, len);
			}
			//关闭流
			in.close();
			out.close();
		}catch (Exception e) {
			return "{\r\n" + 
					"  \"code\": -1\r\n" + 
					"  ,\"msg\": \"error\"\r\n" + 
					"  ,\"data\": {\r\n" + 
					"    \"src\": \"\"\r\n" + 
					"  }\r\n" + 
					"} ";
		}

		return "{\r\n" + 
				"  \"code\": 0\r\n" + 
				"  ,\"msg\": \"success\"\r\n" + 
				"  ,\"data\": {\r\n" + 
				"    \"src\": \"\"\r\n" + 
				"  }\r\n" + 
				"} ";
	}

}